package com.manage.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.AbstractAction;
import com.manage.cache.SpCodeManager;
import com.manage.cache.SpInfoManager;
import com.manage.model.SpCode;
import com.manage.model.SpCodeProvince;
import com.manage.model.SpInfo;
import com.util.ConfigUtil;
import com.util.Region;
import com.util.RegionUtil;
import com.util.Util;

public class SpCodeAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String type;
	private int spId;
	private String codeType;
	private SpCode model;
	private SpInfo spInfo;
	private List<SpCode> list;
	private List<SpCodeProvince> plist;
	SpCodeManager spcodem ;
	SpInfoManager spM;
	private String province;
	private String regions;
	List<Region> regionlist;
	String startdate1;
	String startdate2;
	String expireddate1;
	String expireddate2;
	Map<String,String> parameterMap = new HashMap<String,String>();
	public SpCodeAction()
	{
		spcodem = SpCodeManager.getInstance();
		spM = SpInfoManager.getInstance();
	}
	public String list()
	{
		spInfo = spM.getSpInfo(spId);
		list = spcodem.getSpCodes(spId, codeType);
		return SUCCESS;
	}
	public String show()
	{
		if (model.getId() > 0)
		{
			model = spcodem.getSpCode(model.getId());
			plist = spcodem.getSpCodeProvinces(model.getId());
			type = "update";
			//model.setBasepayaction(model.getBasepayaction().replaceAll("\r\n", "</br>"));
			if(model.getStartdate()!= null && !"".equals(model.getStartdate()) && model.getStartdate().length()>=19)
			{
				startdate1 = model.getStartdate().substring(0,10);
				startdate2 = "T"+model.getStartdate().substring(11,19);
			}
			if(model.getExpireddate()!= null && !"".equals(model.getExpireddate()) && model.getExpireddate().length()>=19)
			{
				expireddate1 = model.getExpireddate().substring(0,10);
				expireddate2 = "T"+model.getExpireddate().substring(11,19);
			}
			model.setBasepayaction(model.getBasepayaction().replace((char)13, '\n')); 
			
			if(model.getCodeType().equals("13")){
				String parameter = model.getBaseparameter();
				if(parameter.trim().length()>0){
					parameter = parameter.replace("{","").replace("}","");
					String[] arr = parameter.split(",");
					for(int i=0;i<arr.length;i++){
						String []s = arr[i].split("=",2);
						parameterMap.put(s[0], s[1]);
					}
				}
			}
		}
		else
		{
			model.setId(-1);
			model.setCodeStatus("2");

			Calendar cal = Calendar.getInstance();
			startdate1 = Util.dateToString(cal.getTime(),"yyyy-MM-dd");
			startdate2 = "T00:00:00";
			cal.add(Calendar.YEAR, 1);
			expireddate1 =Util.dateToString(cal.getTime(),"yyyy-MM-dd");
			expireddate2 ="T23:59:59";
			type = "insert";
		}
		return SUCCESS;
	}
	public String save()
	{
		//spcodem.saveSpCode(codeType, model, plist);
		if (type.equals("offline") || type.equals("online") || type.equals("test"))
		{
			spcodem.saveSpCode(type, model, null);
			model = spcodem.getSpCode(model.getId());
		}
		else
		{
			model.setStartdate(startdate1+" "+startdate2.replace("T", ""));
			model.setExpireddate(expireddate1+" "+expireddate2.replace("T", ""));
			String createDate = Util.dateToString(new Date());
			String createUser = operator.getName();
			model.setOpenProvince(model.getOpenProvince().replaceAll(" ", ""));
			if(type.equals("insert"))
			{
				//model.setCodeStatus("0");
				model.setCreateDate(createDate);
				model.setCreateUser(createUser);
			}
			plist = new ArrayList<SpCodeProvince>();
			if(model.getOpenProvince()!=null && model.getOpenProvince().length()>0)
			{
				String []provincestr = model.getOpenProvince().split(",");
				SpCodeProvince scp = null;
				for(int i =0;i<provincestr.length;i++)
				{
					scp = new SpCodeProvince();
					String fee =  getParameter("open_province_"+provincestr[i].trim()+"_fee_value").toString();
					String dailyLimit = getParameter("open_province_"+provincestr[i].trim()+"_daily_limit").toString();
					String monthlyLimit = getParameter("open_province_"+provincestr[i].trim()+"_monthly_limit").toString();
					String dailyCapacity = getParameter("open_province_"+provincestr[i].trim()+"_daily_capacity").toString();
					String monthlyCapacity = getParameter("open_province_"+provincestr[i].trim()+"_monthly_capacity").toString();
					String shieldRegion = getParameter("open_province_"+provincestr[i].trim()+"_shield_region").toString();
					
					String pbalancetype = getParameter("open_province_"+provincestr[i].trim()+"_pbalancetype").toString();
					String pbalancevalue = getParameter("open_province_"+provincestr[i].trim()+"_pbalancevalue").toString();
					String pbalancecyc = getParameter("open_province_"+provincestr[i].trim()+"_pbalancecyc").toString();
					
					scp.setSpId(model.getSpId());
					scp.setProvinceCode(provincestr[i].trim());
					scp.setShieldRegion(shieldRegion);
					scp.setFeeValue(fee);
					scp.setDailyLimit(dailyLimit);
					scp.setMonthlyLimit(monthlyLimit);
					scp.setDailyCapacity(dailyCapacity);
					scp.setMonthlyCapacity(monthlyCapacity);
					scp.setPbalancetype(pbalancetype);
					scp.setPbalancevalue(pbalancevalue);
					scp.setPbalancecyc(pbalancecyc);
					scp.setCreateDate(model.getCreateDate());
					scp.setCreateUser(model.getCreateUser());
					scp.setCodeStatus(model.getCodeStatus());
					plist.add(scp);
				}
			}
			//游戏基地
			if(model.getCodeType().equals("13")){
				String []arr ={"pid","contentId","channelId","cpId","loginParam","cmSDKVer","chargeId","chargeParam","game_name","charge_name","isOnline","type","pay_type"};
				StringBuilder str=new StringBuilder("{");
				for (int i=0;i<arr.length;i++) {
					String s = arr[i];
					str.append(s+"="+getParameter("tmp_"+s));
					if(i!=arr.length-1){
						str.append(",");
					}else{
						str.append(",url="+ConfigUtil.get("manage", "gamebase.url")+"}");
					}		
				}
				System.out.println("STR:"+str);
				model.setBaseparameter(str.toString());
			}
			spcodem.saveSpCode(type, model, plist);
		}
		return SUCCESS;
	}
	public String regionshow()
	{
		regionlist = RegionUtil.getInstance().getRegion(province);
		return SUCCESS;
	}
	public SpCodeProvince getSpCodeProvince(SpCode code, String province, List<SpCodeProvince> list)
	{
		if(list!=null)
		{
			for (SpCodeProvince p : list)
			{
				if (p.getProvinceCode().equals(province))
					return p;
			}
		}
		
		return new SpCodeProvince(province, code);
	}
	
	
	public String getExpireddate1() {
		return expireddate1;
	}
	public void setExpireddate1(String expireddate1) {
		this.expireddate1 = expireddate1;
	}
	public String getExpireddate2() {
		return expireddate2;
	}
	public void setExpireddate2(String expireddate2) {
		this.expireddate2 = expireddate2;
	}
	public String getStartdate1() {
		return startdate1;
	}
	public void setStartdate1(String startdate1) {
		this.startdate1 = startdate1;
	}
	public String getStartdate2() {
		return startdate2;
	}
	public void setStartdate2(String startdate2) {
		this.startdate2 = startdate2;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getSpId() {
		return spId;
	}
	public void setSpId(int spId) {
		this.spId = spId;
	}
	public String getCodeType() {
		return codeType;
	}
	public void setCodeType(String codeType) {
		this.codeType = codeType;
	}
	public SpCode getModel() {
		return model;
	}
	public void setModel(SpCode model) {
		this.model = model;
	}
	public List<SpCode> getList() {
		return list;
	}
	public void setList(List<SpCode> list) {
		this.list = list;
	}
	public List<SpCodeProvince> getPlist() {
		return plist;
	}
	public void setPlist(List<SpCodeProvince> plist) {
		this.plist = plist;
	}
	public SpInfo getSpInfo() {
		return spInfo;
	}
	public void setSpInfo(SpInfo spInfo) {
		this.spInfo = spInfo;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	
	public String getRegions() {
		return regions;
	}
	public void setRegions(String regions) {
		this.regions = regions;
	}
	public List<Region> getRegionlist() {
		return regionlist;
	}
	public void setRegionlist(List<Region> regionlist) {
		this.regionlist = regionlist;
	}
	public Map<String, String> getParameterMap() {
		return parameterMap;
	}
	public void setParameterMap(Map<String, String> parameterMap) {
		this.parameterMap = parameterMap;
	}
	
	
}
